package com.base.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.jws.WebService;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.base.enums.ResultStatus;
import com.base.model.PROrderDataResult;
import com.base.model.prorder.PROrderData;
import com.base.model.prorder.PROrderDto;
import com.base.model.prorder.PROrderHeadDataItem;
import com.base.model.prorder.PROrderProductDataItem;
import com.base.model.prorder.PROrderRowDataItem;
import com.base.service.PROrderService;
import com.base.service.business.PROrderDataBusiness;

import lombok.extern.slf4j.Slf4j;

/**
 * PR单数据下发实现
 * 
 * @ClassName: IPROrderDataImpl
 * @Description:
 * @Author xuyou
 * @DateTime 2019年10月29日 上午10:33:43
 */
@WebService(
		targetNamespace = "http://office.lifecycle.cn:9001/b2b/ws", 
		serviceName = "PROrderServiceImpl", 
		portName = "prOrderPortName", 
		endpointInterface = "com.base.service.PROrderService")
@Service
@Slf4j
public class PROrderServiceImpl implements PROrderService {

	@Autowired
	private PROrderDataBusiness prOrderDataBusiness;
	@Override
	public List<PROrderDataResult> pushPROrderData(PROrderData prOrderData) {
		List<PROrderDataResult> results = new ArrayList<PROrderDataResult>();
		if (prOrderData == null || CollectionUtils.isEmpty(prOrderData.getPROrderHeadDataItems())) {
			log.warn("未获取到数据");
			PROrderDataResult result = new PROrderDataResult();
			result.setZSUCCESS(ResultStatus.E.getValue());
			result.setZMESSAGE("未获取到数据");
			results.add(result);
			return results;
		}
		// PR单主数据
		List<PROrderHeadDataItem> orderHeadDataItems = prOrderData.getPROrderHeadDataItems();
		log.info("PR单主数据同步数量:{}", orderHeadDataItems.size());
		// 添加PR主数据
		for (PROrderHeadDataItem orderHeadData : orderHeadDataItems) {
			PROrderDto dto = new PROrderDto();
			PROrderDataResult result = new PROrderDataResult();
			// 订货单号
			String zddh = orderHeadData.getZDDH();
			result.setZDDH(zddh);
			try {
				// 模号级别
				List<PROrderRowDataItem> prOrderRowDataItems = prOrderData.getPROrderRowDataItems();
				// 模号级别
				List<PROrderRowDataItem> tempRow = new ArrayList<PROrderRowDataItem>();
				if (!CollectionUtils.isEmpty(prOrderRowDataItems)) {
					for (PROrderRowDataItem rowData : prOrderRowDataItems) {
						if (StringUtils.isNotBlank(rowData.getZDDH()) && rowData.getZDDH().equals(zddh)) {
							// 添加PR单模号级别数据
							tempRow.add(rowData);
						}
					}
				}
				// 单品级别
				List<PROrderProductDataItem> tempProduct = new ArrayList<PROrderProductDataItem>();
				List<PROrderProductDataItem> prOrderProductDataItems = prOrderData.getPROrderProductDataItems();
				if (!CollectionUtils.isEmpty(prOrderProductDataItems)) {
					for (PROrderProductDataItem productData : prOrderProductDataItems) {
						boolean flag = true;
						if (StringUtils.isNotBlank(productData.getZDDH()) && productData.getZDDH().equals(zddh)) {
							// 添加PR单单品级别数据
							tempProduct.add(productData);
							// 如果推送的主数据包含单品级别的数据
							if (flag) {
								orderHeadData.setISSUCCESS("Y");
								flag = false;
							}
						}
					}
				}
				// 添加PR单主数据
				dto.setHeadData(orderHeadData);
				// 添加PR单模号数据
				dto.setRowData(tempRow);
				// 添加PR单单品数据
				dto.setProductData(tempProduct);
				prOrderDataBusiness.save(dto);
			} catch (Exception e) {
				log.error("添加PR单数据失败", e);
				String message = "";
				if (e.getCause() == null) {
					message = e.getMessage();
				} else {
					message = e.getCause().getMessage();
					if (message.contains("无法将 NULL")) {
						message = "必填字段不能为空";
					} else if (message.contains("\n")) {
						message = message.substring(message.indexOf(":") + 1, message.indexOf("\n"));
					} else {
						message = e.getCause().getMessage().replace("\n", "");
					}
				}
				result.setZMESSAGE(message);
				result.setZSUCCESS(ResultStatus.E.getValue());
			}
			results.add(result);
		}
		return results;
	}
}
